package com.rbac.controller;

import com.rbac.common.Result;
import com.rbac.common.RedisKey;
import com.rbac.mapper.PermissionMapper;
import com.rbac.mapper.RoleMapper;
import com.rbac.mapper.UserMapper;
import com.rbac.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/dashboard")
public class DashboardController {
	private final UserMapper userMapper;
	private final RoleMapper roleMapper;
	private final PermissionMapper permissionMapper;
	
	@Autowired
	private RedisService redisService;

	public DashboardController(UserMapper userMapper, RoleMapper roleMapper, PermissionMapper permissionMapper) {
		this.userMapper = userMapper;
		this.roleMapper = roleMapper;
		this.permissionMapper = permissionMapper;
	}

	@GetMapping("/stats")
	public Result<Map<String, Long>> stats() {
		Map<String, Long> cached = redisService.getJson(RedisKey.DASHBOARD_STATS, Map.class);
		if (cached != null) {
			return Result.success(cached);
		}
		long userCount = userMapper.countAll();
		long roleCount = roleMapper.countAll();
		long permissionCount = permissionMapper.countAll();
		long onlineCount = 1L; // JWT 无会话，这里先返回示例值
		Map<String, Long> data = new HashMap<String, Long>();
		data.put("userCount", userCount);
		data.put("roleCount", roleCount);
		data.put("permissionCount", permissionCount);
		data.put("onlineCount", onlineCount);
		redisService.setJson(RedisKey.DASHBOARD_STATS, data, null);
		return Result.success(data);
	}
} 